package com.example.weight.greenSeasons.compost;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class DuifeiMonthMaintanenceNavActivity extends AppCompatActivity {

    private String name, role, factory;

    private Button fandaoji, dundaixitong2, dundaixitong3, dundaixitong4, fuliaoshusongxitong, hunliaojiaobanluoxuan,
            hunliaoshusongluoxuan, niliaoshusongluoxuan, duifeichejianbaoqixitong, duifeichejianchuchoufengji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duifei_month_maintanence_nav);

        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        role = previousIntent.getStringExtra("role");
        factory = previousIntent.getStringExtra("factory");

        TitleBar titleBar = findViewById(R.id.duifei_month_nav_titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });

        fandaoji = findViewById(R.id.duifei_month_nav_fandaoji);
        dundaixitong2 = findViewById(R.id.duifei_month_nav_dundaixitong2);
        dundaixitong3 = findViewById(R.id.duifei_month_nav_dundaixitong3);
        dundaixitong4 = findViewById(R.id.duifei_month_nav_dundaixitong4);
        fuliaoshusongxitong = findViewById(R.id.duifei_month_nav_fuliaoshusongxitong);
        hunliaojiaobanluoxuan = findViewById(R.id.duifei_month_nav_hunliaojiaobanluoxuan);
        hunliaoshusongluoxuan = findViewById(R.id.duifei_month_nav_hunliaoshusongluoxuan);
        niliaoshusongluoxuan = findViewById(R.id.duifei_month_nav_niliaoshusongluoxuan);
        duifeichejianbaoqixitong = findViewById(R.id.duifei_month_nav_duifeichejianbaoqixitong);
        duifeichejianchuchoufengji = findViewById(R.id.duifei_month_nav_duifeichejianchuchoufengji);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/shlj/sjqCompostProduceMaintenance/is_month_complete";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (!jsonObject.isNull("data")) {
                    JSONArray data = jsonObject.getJSONArray("data");
                    int checkedColor = Color.parseColor("#66CC66");
                    int halfCheckedColor = Color.parseColor("#FF9966");
                    int i = 0;
                    if (data.getInt(i++) == 2)
                        fandaoji.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        fandaoji.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        dundaixitong2.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        dundaixitong2.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        dundaixitong3.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        dundaixitong3.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        dundaixitong4.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        dundaixitong4.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        fuliaoshusongxitong.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        fuliaoshusongxitong.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        hunliaojiaobanluoxuan.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        hunliaojiaobanluoxuan.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        hunliaoshusongluoxuan.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        hunliaoshusongluoxuan.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        niliaoshusongluoxuan.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        niliaoshusongluoxuan.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) == 2)
                        duifeichejianbaoqixitong.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        duifeichejianbaoqixitong.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i) == 2)
                        duifeichejianchuchoufengji.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        duifeichejianchuchoufengji.setBackgroundColor(halfCheckedColor);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }, error -> Toast.makeText(DuifeiMonthMaintanenceNavActivity.this,
                "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show()) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<>();
                map.put("equipmentsName", "1#吨袋系统_2#吨袋系统_3#吨袋系统_4#吨袋系统_辅料输送系统_" +
                        "混料搅拌螺旋_混料输送螺旋_泥料输送螺旋_堆肥车间曝气系统_堆肥车间除臭风机");
                map.put("menstanceNum", "3_3_3_3_4_4_4_4_4_4");
                map.put("role", role.equals("操作人")?"operator":"examiner");
                map.put("factory", factory);
                return map;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            boolean needRefresh = data.getBooleanExtra("needRefresh", true);
            if (needRefresh)
                refresh();
        }
    }

    private void go(String fName) {
        Intent intent;
        if (role.equals("操作人"))
            intent = new Intent(this, CompostMonthMaintenanceActivity.class);
        else
            intent = new Intent(this, CompostMonthInspectorActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("fName", fName);
        intent.putExtra("factory",factory);
        startActivityForResult(intent, 1);
    }

    public void fandaoji(View view){ go("1#吨袋系统"); }
    public void dundaixitong2(View view){ go("2#吨袋系统"); }
    public void dundaixitong3(View view){ go("3#吨袋系统"); }
    public void dundaixitong4(View view){ go("4#吨袋系统"); }
    public void fuliaoshusongxitong(View view){ go("辅料输送系统"); }
    public void hunliaojiaobanluoxuan(View view){ go("混料搅拌螺旋"); }
    public void hunliaoshusongluoxuan(View view){ go("混料输送螺旋"); }
    public void niliaoshusongluoxuan(View view){ go("泥料输送螺旋"); }
    public void duifeichejianbaoqixitong(View view){ go("堆肥车间曝气系统"); }
    public void duifeichejianchuchoufengji(View view){ go("堆肥车间除臭风机"); }

    // 刷新activity
    public void refresh() {
        finish();
        Intent intent = new Intent(DuifeiMonthMaintanenceNavActivity.this,
                DuifeiMonthMaintanenceNavActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("factory",factory);
        startActivity(intent);
    }
}